<?php
// @formatter:off
/**
 * @file WCSearchItems.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-24 23:14:00 +0000 (Fri, 24 May 2013) $
 *
 * $Id: WCSearchItems.php 66 2013-05-24 23:14:00Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/search/WCSearchItems.php $
 */
// @formatter:on

require_once dirname(__FILE__)."/WCSearchItems.php";

/**
 * @class WCSearchItem
 */
class WCSearchItem {
	protected $_group = null;
	protected $_info = null;
	protected $_hash = false;
	protected $_broken = true;
	protected $_id = -1;
	protected $_name = "unknown";
	protected $_fullName = "unknown";
	protected $_pattern = false;
	protected $_strPattern = false;
	protected $_filteredPattern = false;
	protected $_filteredHash = 0;

	public function __construct(WCGroup &$group, $info, $hash) {
		$this->_group = $group;
		$this->_info = $info;
		$this->_hash = $hash;
		$this->loadExtraInfo();
	}
	public function __destruct() {
	}
	//
	// Public Methods
	public function count() {
		return false;
	}
	public function factor() {
		$factor = $this->_info["factors"]["e-words"];
		$factor .= ".".$this->_info["factors"]["p-words"];
		$factor .= ".".$this->_info["factors"]["hits"];
		$factor .= ".".$this->_info["factors"]["type"];
		return $factor;
	}
	public function filteredHash() {
		$this->updatePatterns();
		return $this->_filteredHash;
	}
	public function filteredPattern() {
		$this->updatePatterns();
		return $this->_filteredPattern;
	}
	public function fullName() {
		return $this->_fullName;
	}
	public function hasPreviews() {
		return false;
	}
	public function hits() {
		return $this->_info["factors"]["hits"];
	}
	public function isBroken() {
		return $this->_broken;
	}
	public function link() {
		trigger_error(__CLASS__."::".__FUNCTION__."(): Subclass Responsibility.", E_USER_ERROR);
		return "";
	}
	public function name() {
		return $this->_name;
	}
	public function pattern() {
		$this->updatePatterns();
		return $this->_pattern;
	}
	public function previews() {
		return array();
	}
	public function searchPattern() {
		return $this->_info["words"];
	}
	public function type() {
		return $this->type;
	}
	//
	// Protected Methods
	protected function loadExtraInfo() {
		trigger_error(__CLASS__."::".__FUNCTION__."(): Subclass Responsibility.", E_USER_ERROR);
	}
	protected function updatePatterns() {
		if(!$this->_pattern) {
			$this->_pattern = WCSearchFactory::StringToPatterns($this->_strPattern);

			$this->_filteredPattern = array();
			foreach($this->_pattern as $word) {
				$this->_filteredHash * 2;
				if(in_array($word, $this->searchPattern())) {
					$this->_filteredPattern[] = $word;
					$this->_filteredHash += 1;
				}
			}
			while($this->_filteredHash > 0 && !($this->_filteredHash & 1)) {
				$this->_filteredHash /= 2;
			}
		}
	}
}

/**
 * class WCSearchItemCX
 */
class WCSearchItemCX extends WCSearchItem {
	/**
	 * @var WCComic
	 */
	protected $_comic = false;

	public function hasPreviews() {
		return true;
	}
	public function link() {
		return "?action=".WC_ACTION_COMIC_SHOW."&group={$this->_comic->group}&comic={$this->_comic->id}";
	}
	public function previews() {
		return array($this->_comic->previewUri());
	}
	public function type() {
		return "Comic";
	}
	protected function loadExtraInfo() {
		global $wcComicsHolder;

		$this->_comic = $wcComicsHolder->item($this->_info["id"], $this->_group);
		if($this->_comic && $this->_comic->ok()) {
			$this->_broken = false;
			$this->_id = $this->_comic->id;
			$this->_name = $this->_comic->name();
			$this->_fullName = "{$this->_comic->name()} at {$this->_comic->directory()->name}";
			$this->_strPattern = "{$this->_comic->directory}/{$this->_comic->name()}";
		} else {
			$this->_comic = false;
		}
	}
}

/**
 * class WCSearchItemDI
 */
class WCSearchItemDI extends WCSearchItem {
	/**
	 * @var WCDirectory
	 */
	protected $_directory = false;

	public function count() {
		$out = false;

		if(!$this->isBroken()) {
			$out = $this->_directory->count;
		}

		return $out;
	}
	public function hasPreviews() {
		return true;
	}
	public function link() {
		return "?action=".WC_ACTION_DIRECTORY_SHOW."&group={$this->_directory->group}&directory={$this->_directory->id}";
	}
	public function previews() {
		return $this->_directory->previews();
	}
	public function type() {
		return "Directory";
	}
	protected function loadExtraInfo() {
		global $wcDirectoriesHolder;

		$this->_directory = $wcDirectoriesHolder->item($this->_info["id"], $this->_group);
		if($this->_directory) {
			$this->_broken = false;
			$this->_id = $this->_directory->id;
			$this->_name = $this->_directory->name;
			$this->_fullName = $this->_directory->name;
			$this->_strPattern = $this->_directory->name;
		} else {
			$this->_directory = false;
		}
	}
}

/**
 * class WCSearchItemTA
 */
class WCSearchItemTA extends WCSearchItem {
	/**
	 * @var WCTag
	 */
	protected $_tag = false;

	public function count() {
		$out = false;

		if(!$this->isBroken()) {
			$out = $this->_tag->count;
		}

		return $out;
	}
	public function hasPreviews() {
		return true;
	}
	public function link() {
		return "?action=".WC_ACTION_TAG_SHOW."&group={$this->_tag->group}&tag={$this->_tag->id}";
	}
	public function previews() {
		return $this->_tag->previews();
	}
	public function type() {
		return "Tag";
	}
	protected function loadExtraInfo() {
		global $wcTagsHolder;

		$this->_tag = $wcTagsHolder->item($this->_info["id"], $this->_group);
		if($this->_tag) {
			$this->_broken = false;
			$this->_id = $this->_tag->id;
			$this->_name = $this->_tag->name;
			$this->_fullName = $this->_tag->name;
			$this->_strPattern = $this->_tag->name;
		} else {
			$this->_tag = false;
		}
	}
}

/**
 * class WCSearchItemCL
 */
class WCSearchItemCL extends WCSearchItem {
	/**
	 * @var WCCollection
	 */
	protected $_collection = false;

	public function count() {
		$out = false;

		if(!$this->isBroken()) {
			//$out = $this->_collection->count(); //@TODO
		}

		return $out;
	}
	public function hasPreviews() {
		return true;
	}
	public function link() {
		return "?action=".WC_ACTION_COLLECTION_SHOW."&group={$this->_collection->group}&collection={$this->_collection->id}";
	}
	public function previews() {
		return $this->_collection->previews();
	}
	public function type() {
		return "Collection";
	}
	protected function loadExtraInfo() {
		global $wcCollectionsHolder;

		$this->_collection = $wcCollectionsHolder->item($this->_info["id"], $this->_group);
		if($this->_collection) {
			$this->_broken = false;
			$this->_id = $this->_collection->id;
			$this->_name = $this->_collection->name;
			$this->_fullName = $this->_collection->name;
			$this->_strPattern = $this->_collection->name;
		} else {
			$this->_collection = false;
		}
	}
}

/**
 * class WCSearchItemRL
 */
class WCSearchItemRL extends WCSearchItem {
	/**
	 * @var WCRule
	 */
	protected $_rule = null;

	public function count() {
		$out = false;

		if(!$this->isBroken()) {
			//$out = $this->_collection->count(); //@TODO
		}

		return $out;
	}
	public function hasPreviews() {
		return false;
	}
	public function link($mobile = false) {
		return "?action=".WC_ACTION_RULE_SHOW."&group={$this->_rule->group}&rule={$this->_rule->id}";
	}
	public function previews() {
		return array();
	}
	public function type() {
		return "Rule";
	}
	protected function loadExtraInfo() {
		global $wcRulesHolder;

		$this->_rule = $wcRulesHolder->item($this->_info["id"], $this->_group);
		if($this->_rule) {
			$this->_broken = false;
			$this->_id = $this->_rule->id;
			$this->_name = $this->_rule->name;
			$this->_fullName = $this->_rule->name;
			$this->_strPattern = $this->_rule->name;
		} else {
			$this->_rule = false;
		}
	}
}
